package com.keyue.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keyue.common.constant.Constant;
import com.keyue.common.constant.ServerResult;
import com.keyue.domain.dto.BaseDO;
import com.keyue.domain.dto.admin.AdminJobDO;
import com.keyue.domain.vo.admin.AdminJobQueryVO;
import com.keyue.domain.vo.admin.AdminJobResultVO;
import com.keyue.domain.vo.admin.AdminJobVO;
import com.keyue.mapper.admin.AdminJobMapper;
import com.keyue.service.admin.AdminJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 岗位表(AdminJob)表服务实现类
 *
 * @author makejava
 * @since 2023-08-30 17:51:48
 */
@Slf4j
@Service
public class AdminJobServiceImpl extends ServiceImpl<AdminJobMapper, AdminJobDO> implements AdminJobService {


    /**
     * 通过ID查询单条数据
     * <p>
     * adminJobQueryVO 主键
     *
     * @return 实例对象
     */
    @Override
    public ServerResult queryById(AdminJobQueryVO adminJobQueryVO) {
        if (Objects.isNull(adminJobQueryVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        AdminJobDO AdminJobDo = getById(adminJobQueryVO.getId());
        if (Objects.isNull(AdminJobDo)) {
            return ServerResult.success(null);
        }
        return ServerResult.success(BeanUtil.copyProperties(AdminJobDo, AdminJobVO.class));
    }

    /**
     * 分页查询
     *
     * @param queryVO 筛选条件
     * @return 查询结果
     */
    @Override
    public ServerResult list(AdminJobQueryVO queryVO) {

        LambdaQueryWrapper<AdminJobDO> queryWrapper = buildQueryByParam(queryVO);
        Page page = new Page<>(queryVO.getPage(), queryVO.getPageSize());
        IPage<AdminJobDO> iPage = page(page, queryWrapper);

        Page<AdminJobResultVO> webVOPage = new Page<>();
        List<AdminJobDO> adminJobDOList = iPage.getRecords();
        if (CollectionUtils.isNotEmpty(adminJobDOList)) {
            List<AdminJobResultVO> queryVos = adminJobDOList.stream().map(x -> {
                AdminJobResultVO resultVO = new AdminJobResultVO();
                BeanUtils.copyProperties(x, resultVO);
                return resultVO;
            }).collect(Collectors.toList());
            webVOPage.setRecords(queryVos);
        }
        BeanUtils.copyProperties(page, webVOPage, "records");
        return ServerResult.success(webVOPage);
    }


    /**
     * @param queryVO
     * @return
     */
    private LambdaQueryWrapper<AdminJobDO> buildQueryByParam(AdminJobQueryVO queryVO) {
        return new LambdaQueryWrapper<AdminJobDO>()
                .eq(BaseDO::getEnableFlag, Constant.ENABLE_TEN)
                .eq(StringUtils.isNotBlank(queryVO.getName()), AdminJobDO::getName, queryVO.getName());
    }

    /**
     * 新增数据
     *
     * @param adminJobVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult add(AdminJobVO adminJobVO) {
        //todo 校验
        if (Objects.nonNull(adminJobVO.getId())) {
            adminJobVO.setId(null);
        }
        AdminJobDO adminJobDo = BeanUtil.copyProperties(adminJobVO, AdminJobDO.class);

        save(adminJobDo);

        return ServerResult.success();
    }

    /**
     * 修改数据
     *
     * @param adminJobVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult edit(AdminJobVO adminJobVO) {
        //todo 校验
        if (Objects.isNull(adminJobVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        AdminJobDO adminJobDo = BeanUtil.copyProperties(adminJobVO, AdminJobDO.class);

        updateById(adminJobDo);

        return ServerResult.success();
    }

    /**
     * 通过主键删除数据
     *
     * @param adminJobQueryVO 筛选条件
     * @return 是否成功
     */
    @Override
    public ServerResult deleteById(AdminJobQueryVO adminJobQueryVO) {
        List<Long> ids = adminJobQueryVO.getIds();
        if (CollUtil.isEmpty(ids)) {
            return ServerResult.fail("ids 参数不能为空");
        }
        List<AdminJobDO> adminJobList = listByIds(ids);
        if (CollUtil.isEmpty(adminJobList)) {
            return ServerResult.success();
        }
        //todo 校验是否可以逻辑删除
        LambdaUpdateWrapper<AdminJobDO> updateWrapper = new LambdaUpdateWrapper<AdminJobDO>()
                .in(AdminJobDO::getId, ids)
                .set(AdminJobDO::getEnableFlag, Constant.UNABLE_TWENTY);
        update(updateWrapper);
        return ServerResult.success();
    }
}
